import { Injectable, Inject, BadRequestException } from '@nestjs/common'
import { ModeService } from 'src/database/mode/mode.service'
import { RelationService } from 'src/database/relation/relation.service'
import { CreateTaskDto } from 'src/database/task/dto/create-task.dto'
import { TaskService } from 'src/database/task/task.service'
import { UserService } from 'src/database/user/user.service'
import { Repository } from 'typeorm'
@Injectable()
export class NormalService {
  // 引入数据模块的服务
  constructor(
    private readonly modeService: ModeService,
    private readonly relationService: RelationService,
    private readonly taskService: TaskService,
    private readonly userService: UserService,
    // @Inject('TASK_REPOSITORY')
    // private readonly taskRepository: Repository<Task>,
  ) { }

  // 创建任务
  async createTask(task: any, uid: number) {
    // 不报错则创建成功
    await this.taskService.create({ ...task, uid })
    // 返回结果
    return { error: null, msg: null }
  }


  async findTask(uid) {
    return await this.taskService.find({ where: { uid } })
  }

  // 完成指定uid的用户的指定tid的任务
  async finishTask(tid) {
    return await this.taskService.update(tid, { state: 2})
  }


  async removeTask(id: number) {
    let res = undefined
    try {
      // res = await this.taskRepository.delete({ tid: id });
      res = await this.taskService.remove(id)
    } catch (error) {
      throw new BadRequestException('删除任务数据错误：' + error)
    }
    return res
  }

  // 更新任务等级
  async updateTaskLevel(tid:number, level:number){
    return await this.taskService.update(tid, {level:level});
  }
}
